
function toggleBox(elm) {
	if (cls.has(elm.parentNode,"closed")) {
		cls.replace(elm.parentNode,"closed","opened");
	} else cls.replace(elm.parentNode,"opened","closed");
}

browser =
{
	
	onLoad:function(e)
	{
		browser.ajaxifyLinks(document);
		browser.ajaxifyForms(document.getElementById("browser"));
		Nifty("div#right");
		Nifty("div.box");
		//Nifty("div#hotlinks");
		//Nifty("div.box a.active","tl bl");
		//Nifty("div.group a");
	},

	ajaxifyLinks:function(el)
	{
		anchors = el.getElementsByTagName("a");
		for (y in anchors) {
			YAHOO.util.Event.addListener(anchors[y], "click", controller.clickAt2);
		}		
	},

	ajaxifyForms:function(el)
	{
		els = el.getElementsByTagName("input");
		for (i in els) {
			if (els[i]!=null)
			if (els[i].type=="submit" && els[i].beenThere==null) 
			{				
				///yeaaaa, opera bere i nepárové tagy dvakrát, no žůžo héj
				els[i].beenThere=true;
				YAHOO.util.Event.addListener(els[i], "click", controller.sendPost);
			}
		}		
	},

	switchGroup : function(view)
	{
		//alert("shiiii");
		this.browse(view);
		this.hotlinks(view);
		this.tree(view); 
		/*
		Nifty("div#right");
		Nifty("div#tree");
		Nifty("div#hotlinks");
		*/
		//Nifty("div.tree a.active","tl bl");
	},

	handleFailure : function(o)
	{
		alert("chyba chybá! "+o.status);
		if(o.responseText !== undefined) {
			div=document.getElementById("browser");	
			s = o.responseText;
			div.innerHTML = s+div.innerHTML;
		}
	},
	
	hotlinks : function(view) { this.request(view, "hotlinks"); 	},
	_hotlinks : function(o)
	{
		if (o.responseText !== undefined) {
			if (o.responseText=="") {
				document.getElementById("hotlinks").parentNode.style.display="none";
			} else {
				document.getElementById("hotlinks").parentNode.style.display="block";
				div=document.getElementById("hotlinks");
				div.innerHTML = o.responseText;
				browser.ajaxifyLinks(div);
			}
		}
	},

	tree : function(view) { this.request(view, "tree"); },
	_tree : function(o)
	{
		if (o.responseText !== undefined) {
		if (o.responseText !== undefined) {
			if (o.responseText=="") {
				document.getElementById("tree").parentNode.style.display="none";
			} else {
				document.getElementById("tree").parentNode.style.display="block";			}
				div=document.getElementById("tree");
				div.innerHTML = o.responseText;
				browser.ajaxifyLinks(div);
			}	
		}
	},

	browse : function(view) { this.request(view, "browser"); 	},
	_browser : function(o)
	{
		if (o.responseText !== undefined) {
			div=document.getElementById("browser");
			div.innerHTML = o.responseText;

			/*
			for (i = 0; i<10 ; i++) {
				div.innerHTML+='<br />';
			}
			*/

			browser.ajaxifyForms(div);
			browser.ajaxifyLinks(div);
			Haforms.searchArea(div);
		}
	},

	request : function(view, content)
	{
		callback =
		{
  			success: browser["_"+content],
		  	failure: this.handleFailure
		};
		request = YAHOO.util.Connection.asyncRequest('GET', "admin.php?style=AJAX&content="+content+"&"+view.getUrl()+"&fuck_cache="+Math.random(999999999999), callback); 
	}

};



View = function(url)
{
	this.attrs = {};
	
	if (url!=null)
	{
		param=url.split("?");
		if (param[1]!=null) {
			params=param[1].split("&");
			if (params.length!=null) {
				for (i=0; i<params.length; i++) 
				{
					info=params[i].split("=");

					if (info[0]!=null && info[0]!="content") if (info[1]!=null) this.attrs[info[0]] = info[1].toLowerCase();
				};
			}
		}
	};
		
	this.getUrl = function()
	{
		var s = "";
		for (var n in this.attrs) {
			if (this.attrs[n]!=controller.defaultView.attrs[n])
			s+=n+"=" + this.attrs[n]+"&";
		}
		return s.substring(0,s.length-1);
	};

	this.getAnchor = function()
	{
		var s = "";
		for (var n in this.attrs) {
			if (this.attrs[n]!=controller.defaultView.attrs[n])
 			s+=n+":" + this.attrs[n]+",";
		}
		return "#"+s.substring(0,s.length-1);
	};

	this.fromAnchor = function(url)
	{
		param=url.split("#");
		if (param[1]!=null)
		{
			params=param[1].split(",");
			if (params.length!=null)
			{
				for (i=0; i<params.length; i++) {
					info=params[i].split(":");
					this.attrs[info[0]] = info[1];				
				}		
			}
		}	
	};
	
	this.isEqual = function(v) {
		if (v==null) return false;
		for (var n in this.attrs) {
 		   if (this.attrs[n]!=v.attrs[n]) return false;
		}
		for (var n in v.attrs) {
 		   if (this.attrs[n]!=v.attrs[n]) return false;
		}
		return true;
	};
};

historyEvent = {
	timeout:50,
	lasthref:location.href,
	
	isEvent:function() {
		this.start(100);		
		if (this.lasthref!=location.href) {
				//alert("jůů, šels dopředu, nebo dozadu:)");
				controller.historyGo();
		}
		this.lasthref=location.href;
	},

	start:function(timeout) {
		this.timer=setTimeout("historyEvent.isEvent()", timeout);
	},
	stop:function() {
		clearTimeout(this.timer);
	},
	
	fixTimer:function () {
		//v opeře "vypadávalo"
		this.timer2=setTimeout("this.fixTimer()", 3000);
		this.start(100);
	}
}

var controller =
{	
	onLoad:function()
	{
		controller.view = new View(_thisUrl);
		controller.defaultView = new View(_defaultUrl);
		
		urlV = new View(_thisUrl);
		urlV.fromAnchor(location.href);
		
		if (!controller.view.isEqual(urlV)) { 
			controller.changeView(urlV);
		} else location.href = controller.view.getAnchor();
		
		historyEvent.start(500);
	},
	
	logout:function() {
		
		/** TODO : nefunguje, spravit */
		alert();
		top.location.reload();
	},
	
	drag:function(who, where, rel) 
	{
		
		while (who.tagName!="TR" && who.parentNode!=null) who = who.parentNode;
		while (where.tagName!="TR" && where.parentNode!=null) where = where.parentNode;
		
		who = who.getElementsByTagName("input")[0].name;
		where = where.getElementsByTagName("input")[0].name;
		if (who==null || where==null) return;
		
		who = who.substring(6,who.length-2); 
		where = where.substring(6,where.length-2);
		post = "_action[place]=1&rel="+rel+"&id="+who+"&destination="+where+"&keepget=1";
		//alert("dragged "+who+" "+rel+" "+where);
		callback =
		{
	  		success: browser._browser,
		  	failure: browser.handleFailure
		};
		var cObj = YAHOO.util.Connection.asyncRequest('POST', "admin.php?style=AJAX&content=browser&"+controller.view.getUrl()+"&fuck_cache="+Math.random(999999999999), callback, post);
		
	},

	changeView:function(v)
	{
		//historyEvent.start(200);		
		location.href=v.getAnchor();
		historyEvent.lasthref=location.href;

		href=v.getAnchor();
		
		anchors=document.getElementsByTagName("a");
		
		elm=null;
		//alert("budu hledat mezi"+anchors.length+" odkazy.");
		for (u=0; u<anchors.length; u++) {
			cls.remove(anchors[u],"active");
			//alert(anchors[u].href+" "+i);
			av=new View(anchors[u].href);
			if (av.isEqual(v)) {
				elm=anchors[u];
				cls.add(elm,"active");
			} 
		}
		 
		//if (elm==null) { alert("nenašel jsem kliknutého linka"); } 

		if (controller.view.attrs.group!=v.attrs.group && v.attrs.group!=null) {
			browser.switchGroup(v);
		} else if (true || this.view.browse!=v.browse || this.view.id!=v.id || this.view.page!=v.page) {
			browser.browse(v);
		}
		 
		this.view=v;			
	},

	clickAt2 : function(e)
	{
		elm=YAHOO.util.Event.getTarget(e);
		while (elm.href==null) elm = elm.parentNode;
		v = new View(elm.href);
		//alert("dd "+v.getUrl());
		controller.changeView(v);
		YAHOO.util.Event.stopEvent(e);
	},


	sendPost : function(e)
	{
		YAHOO.util.Event.stopEvent(e);
		//return;
		//alert(''+e.target.value);
		button = e.target;
		
		form=button.form;

		inputs = form.getElementsByTagName("input");
		for (i in inputs) {
			if (inputs[i].type == "submit" && inputs[i].value!=button.value) {
				inputs[i].disabled=true;
			}	
		}
		
		YAHOO.util.Connection.setForm(form, true); 
		callback =
		{
	  		upload: browser._browser,
		  	failure: browser.handleFailure,
		  	scope: this
		};

		var cObj = YAHOO.util.Connection.asyncRequest('POST', "admin.php?style=AJAX&content=browser&"+controller.view.getUrl()+"&"+button.name+"=1&fuck_cache="+Math.random(999999999999), callback);
		YAHOO.util.Event.stopEvent(e);
	},

	historyGo : function()
	{
		v = new View(null);
		v.fromAnchor(location.href);
		this.changeView(v);
	}
	
};

YAHOO.util.Event.addListener(window,"load",browser.onLoad);
YAHOO.util.Event.addListener(window,"load",controller.onLoad);


